package io.sage.modules.flow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.sage.modules.flow.service.ReadsenseApiService;
import io.sage.modules.readsenseapi.entity.BFlowCustomerGroups;
import io.sage.modules.readsenseapi.entity.BFlowCustomers;
import io.sage.modules.readsenseapi.vo.CustomerGroupsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.sage.modules.flow.service.FlowCustomerGroupsService;


@Service("flowCustomerGroupsService")
public class FlowCustomerGroupsServiceImpl implements FlowCustomerGroupsService {
    @Autowired
    private ReadsenseApiService readsenseApiService;

    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public Map<String,Object> selectFlowCustomerGroupsList(Map<String, Object> params) {
        //提供api接口地址并得到json字符串
        String uri1="company/customer_groups?&per_page="+params.get("pageSize")+"&page="+params.get("pageNum")+"&group_type=";
        Map<String,String> json = readsenseApiService.gets(uri1);
        //转化为数组
        JSONArray jsonArray = JSONArray.parseArray(json.get("entity"));
        List<BFlowCustomerGroups> bFlowCustomerGroups = jsonArray.toJavaList(BFlowCustomerGroups.class);
        //遍历后得到头像地址
        for (int i=0;i<bFlowCustomerGroups.size();i++){
            BFlowCustomerGroups bFlowCustomerGroups1 = bFlowCustomerGroups.get(i);
            String id = bFlowCustomerGroups1.getId();
            String uri="company/customers?is_vip=true&has_events=&customer_group_id="+id+"&search=";
            Map<String,String> customer=readsenseApiService.gets(uri);
            //转化为数组
            JSONArray jsonArrays = JSONArray.parseArray(customer.get("entity"));
            //转换为集合
            List<BFlowCustomers> bFlowCustomers=jsonArrays.toJavaList(BFlowCustomers.class);
            //赋值
            bFlowCustomerGroups1.setbFlowCustomers(bFlowCustomers);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("total",json.get("total"));
        map.put("list",bFlowCustomerGroups);
        return map;
    }

    /**
     * 获得total值
     * @return
     */
    public String gettotal(){
        //提供api接口地址并得到json字符串
        String uri="company/customer_groups?group_type=";
        Map<String,String> json = readsenseApiService.gets(uri);
        String total=json.get("total");
        System.out.println("total==>"+total);
        return total;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatchIds(String[] ids) {

        String id="";
        for (int i = 0; i < ids.length; i++) {
            id=ids[i];
            String uri="company/customer_groups/"+id;
            System.out.println("id==>"+id);
            String getsagin = readsenseApiService.deletes(uri);
        }
        return 1;
    }

    /**
     * 通过id寻找
     * @param id
     * @return
     */
    @Override
    public BFlowCustomerGroups selectByPrimaryKey(String id) {
        String uri="company/customer_groups/"+id;
        String json = readsenseApiService.getsagin(uri);
        //jaon字符串转换为实体类
        BFlowCustomerGroups bFlowCustomerGroups = JSONObject.toJavaObject(JSONObject.parseObject(json), BFlowCustomerGroups.class);
        return bFlowCustomerGroups;
    }

    /**
     * 修改
     * @param customerGroupsVo
     * @return
     */

    @Override
    public int updateByPrimaryKeySelective(CustomerGroupsVo customerGroupsVo) {
        Map<String,Object> map=new HashMap<>();
        String uri="company/customer_groups";
        map.put("customer_group",customerGroupsVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }
    //会员组添加
    @Override
    public int insert(CustomerGroupsVo customerGroupsVo) {
        Map<String,Object> map=new HashMap<>();
        String uri="company/customer_groups";
        map.put("customer_group",customerGroupsVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }
    /**
     * 详情页展示
     */
    @Override
    public List<BFlowCustomers> selectByBFlowCustomerGroups(String id) {
    String uri = "company/customers/" + id;
    String json = readsenseApiService.getsagin(uri);
    //jaon字符串转换为集合
    JSONArray jsonArray = JSONArray.parseArray(json);
    List<BFlowCustomers> bFlowCustomers = jsonArray.toJavaList(BFlowCustomers.class);
        return bFlowCustomers;
    }
}
